package file.day9_24.服务端和客户端互发消息02;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

public class Server {
    private ServerSocket serverSocket;
    private PrintWriter[] allOut = {};

    public Server(){
        System.out.println("正在启动一个服务器");

        try {
            serverSocket = new ServerSocket(8091);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("启动服务器完毕");
    }

    public void start(){
        try {

            while (true){
                System.out.println("等待客户端的连接");
                Socket socket = serverSocket.accept();
                System.out.println("客户端连接成功");

                //多线程处理
                ClientHandler clientHandler = new ClientHandler(socket);
                Thread t = new Thread(clientHandler);
                t.start();
            }



        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    private class ClientHandler implements Runnable{
        private Socket socket;
        private String host;

        public ClientHandler(Socket socket){
            this.socket = socket;
            host = socket.getInetAddress().getHostName();//获取远端计算机地址信息
        }

        public void run(){
            PrintWriter pw = null;

            try{
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is,"UTF-8");
                BufferedReader br = new BufferedReader(isr);

                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw);

                synchronized (Server.class){
                    allOut = Arrays.copyOf(allOut,allOut.length+1);
                    allOut[allOut.length-1] = pw;
                }


                System.out.println("当前人数"+allOut.length);
                String line;
                while ((line = br.readLine()) != null){
                    System.out.println(host+"say:"+line);
                    System.out.println("接收成功");
                    sendMessage(host+"说："+line);
                }
            }catch (IOException e){
                System.out.println(host+"断了");
            }finally {
                synchronized (Server.this){//同步操作，防止在代替数值内容后被抢，导致数组没缩容
                    for (int i = 0; i < allOut.length; i++) {
                        if(allOut[i] == pw){
                            allOut[i] = allOut[allOut.length-1];
                            allOut = Arrays.copyOf(allOut,allOut.length-1);
                            break;
                        }
                    }
                }
                sendMessage(host+"下线了，当前在线人数"+allOut.length);
            }

        }

        //此方法将会发送信息给所有的客户端
        public void sendMessage(String message){
            synchronized(Server.this){//防止并发产生发送信息没有到到所有人的现象
                for (int i = 0; i < allOut.length; i++) {
                    allOut[i].println(message);
                }
            }

        }
    }
}
